$(function () {
	//系统日志
	//----监管平台日志
	/**
     * Init
     *
     * 
     * |-- 
     * |   `-- 
     * |   `-- 
     * |	   `-- 
     * |		   `-- 
     * |		   `-- 
     * |			   `-- 
     * |		   `-- 
     * |-- 
     * |-- 
     */
	var ad = appData();
	
	
	var tmplObj = null;
	var arryAll = 0;//判断个数
	var idName = [];//动态id名
	var arry1 = [],arry2 = [],arry3 = [],arry4 = [],arry5 = [],arry6 = [],arry7=[];
	var bigArry = [arry1,arry2,arry3,arry4,arry5,arry6,arry7];
	var marks = [];//动态mark标记后台返回
	var firstShow = 0
	var leftName = temp.logTitleArr();
	var EnName = ['HbaseFWRZ','SolrFWRZ','MySQLFWRZ','MongoDBFWRZ','FTPCJRZ',
	'JTTMHbase','JTTMSolr','JTTMMySql','DTTMMySql','WJJTTM','WJDTTM',
	'YWCZRZ',
	'JGPTRZ','TZSBRZ','TMSBRZ','YWSBRZ']
	var pageType = '';
	
	DomNameStart()
	function DomNameStart() {
		
		// $(".advanced-search").toggle(200);
		  $("#achieve").on("click",function(){
		  		firstShow = 1
		  	showTable(true,1)
		   
		  })  
		//侧面添加class索引加载
		if(!$.getUrlParam("id")){
			
			//$('.sys-sidebar ul').children('li').eq(3).find('ul li').eq(0).addClass('active')
			
		//debugger
		var name = $('.sys-sidebar').find('li .active a').text()
		
		$("#TopName").text(name)//替换头部日志名称
			pageType= EnName[leftName.indexOf(name)]
			//debugger
			console.log(pageType)
			$("#TopName").text(name)//替换头部日志名称
		}
		


		
		ad.selectList('HBASE_DATA_TOP',{"pageType":pageType},function(data){
			
						loadDom2(data.keyType,"#keyType",false)
					//	loadDom2(data.result,"#result")
//						loadDom2(data.other[0],"#result",true)
//						loadDom2(data.other[1],"#rank",true)
						
						
			$("#topBody").empty()
			for(var i=0;i<data.list.length;i++){
				

				 function lengths() {
					 	var length = data.list[i].value.length
					 	var strings = '';
					 	for(var j=0;j<length;j++){
					 		strings+=`<li><a href="javascript:void(0)" title="${data.list[i].value[j].titles?data.list[i].value[j].titles:""}" data-id="${data.list[i].value[j].values}";>${data.list[i].value[j].names}</a></li>`
					 	}
					 	return strings
					 }

					arryAll+=1
					idName.push(data.list[i].stringId)
					marks.push(data.list[i].mark)
					temp.type0("#topBody",data.list[i],lengths())

			}
			$(".advanced-search").toggle(200);
			domCase()
		})

		
		
	
			showTable(true, 1)
		
			$.daterRangeDefine("#DateRange")
			$("#DateRange").attr('readonly','readonly').css('width','340px')
		
		
		
		
	}
	
	
	function domCase() {
		$("#search1").off('click').on('click',function(){
			var searchVal  = $("#searchVal").val() 
				firstShow = 1
			showTable(true,1,true)
		})		
		
		var idArry = []
		bigArry.splice(arryAll)
		
		for(var i=0;i<$(".as-item").length;i++){
			if($(".as-item").eq(i).find('ul').attr('id')){
				idArry.push("#"+$(".as-item").eq(i).find('ul').attr('id'))
			}
			
		}
		for(var i=0;i<arryAll;i++){
			
			addDomName(idArry[i],bigArry[i])
		
		}
		
		
		function addDomName(id,arrys) {
			 $(id).on('click','li',function(){
			 
				var _this = $(this);
				if(_this.hasClass('active')){
					_this.removeClass('active')
				}else{
					_this.addClass('active')
				}
				if(arrys.indexOf(_this.find('a').attr('data-id'))==-1){
					arrys.push(_this.find('a').attr('data-id'))
				}else{
					var num =  arrys.indexOf(_this.find('a').attr('data-id'));
	    				arrys.splice(num,1);
				}
				console.log(arrys)
			})
		}
		
	}
	//表格show加载
	function showTable(dontRenderPager, page,all) {

  		
		ad.insert("HBASE_DATA_LIST", createParam(page,all), function(data) {
			
			$("#tableBody").empty();
			$("#tableTr").empty()
//			$("#tableTr").empty().append(`<th class="field no-sort" width="60"><a class="colbtn no-sort" href="javascript:void(0)"><i class="icon-eye-o"></i></a>
//	              <ul class="field-list" id="tbList" >
//	              	
//	              </ul>
//	            </th>`);
			if (data.length === 0) {
				$("#tableBody").append("<tr><td colspan='3' class='colspanCss'><h1>暂无数据</h1></td></tr>");
				return false;
			}
			for(var i=0;i<data.tableHead.length;i++){
				
				$("#tbList").append(`<li class="checked">${data.tableHead[i]}</li>`)
				$("#tableTr").append(`<th>${data.tableHead[i]}</th>`)
			}
			
			for(var i=0;i< data.content.length;i++){
				 
				$("#tableBody").append(`<tr>${lengthsTable(i,data)}</tr>`)
				
			}
			
			//$("#tableBody").empty().append($.tmpl(tmplObj, data.content));
			if (dontRenderPager) {
				renderPager(data.totalRecord);
			}
			tableCut(data.display)
			
			$('table').tablesort().data('tablesort')
			//表格排序
		})
	}
	
	function renderPager(totalRecord, nowPage) {
		$("div.page").pagination(totalRecord, appApi("PAGE_SIZE"), {
			callback: function(page_id, jq) {
				pageNumNow1 = page_id + 1;
				showTable(false, pageNumNow1);
			}
		}, nowPage);
	};
	
	function lengthsTable(i,data) {
					 	var length = data.content[0].length
					 	var strings = '';
					 	var td1 = ['查询','删除','新增','修改']
					 	
					 	var td2 = ['成功','失败']
					 	
					 	var td3 = ['高风险', '中风险', '低风险','其他','一般风险']
					 	
					 	for(var j=0;j<length;j++){
					 	
					 		if(td1.indexOf(data.content[i][j])!=-1){
					 			if(td1.indexOf(data.content[i][j])!=0){
					 				strings+=`<td><span class="fc-danger">${data.content[i][j]}</span></td>`
					 			}else{
					 				strings+=`<td><span class="">${data.content[i][j]}</span></td>`
					 				
					 			}
					 			
					 		}else if(td2.indexOf(data.content[i][j])!=-1){
					 			if(td2.indexOf(data.content[i][j])==0){
					 				strings+=`<td><span class="badge badge-success">${data.content[i][j]}</span></td>`
					 			}
					 			if(td2.indexOf(data.content[i][j])==1){
					 				strings+=`<td><span class="badge badge-danger">${data.content[i][j]}</span></td>`
					 			}	
					 		}else if(td3.indexOf(data.content[i][j])!=-1){
					 			if(td3.indexOf(data.content[i][j])==0){
					 				strings+=`<td><span class="badge fc-danger">${data.content[i][j]}</span></td>`
					 			}
					 			if(td3.indexOf(data.content[i][j])==1){
					 				strings+=`<td><span class="badge fc-warning">${data.content[i][j]}</span></td>`
					 			}	
					 			if(td3.indexOf(data.content[i][j])==2){
					 				strings+=`<td><span class="badge ">${data.content[i][j]}</span></td>`
					 			}
					 		}
					 		else{
					 			strings+=`<td>${data.content[i][j]}</td>`
					 		}
					 		
					 	}
					 	return strings
					 }
	//数据的下载
	$("#loadLog").on("click",function(){
		
		uploadingFile()
		
	})
 
	//数据载入按钮
	function uploadingFile() {
		
		
		var types = $("#TopName").text()
		console.log(types)
		//var param = "key=222";
		
		
		$.downloadInterface($.getRadParamUrl(appApi("T_UPLOADING_FILE")),"get",{name:types});
	};
	
	
	function tableCut(hideArrys) {
		for(var i=0;i<$("#tbList li").length;i++){
			if(hideArrys.indexOf(i)!=-1){
				$("#tbList li").eq(i).removeClass('checked')
				var col_index = i+1
				 $("table").find("thead th:eq("+col_index+")") .hide();
			      $("table").find("tr").each(function() {
			        $(this).children("td:eq("+col_index+")").hide();
			      });
			}
			
				
		}
	
		 $(".field").on("click", "a", function() {
		    $(this).next("ul").slideToggle(200);
		    
		  })
	
		  $(".field").on("click", "li", function() {
		    var col_index = $(this).index()+1;
		    var table=$(this).parents("table");
		    if($(this).hasClass('checked')) {
		      if($(this).parent().find(".checked").length<4){
		      	_as.jAlert("最少保留显示3个字段")
		       // alert("最少保留显示3个字段");
		        return
		      }
		      $(this).removeClass('checked')
		      table.find("thead th:eq("+col_index+")") .hide();
		      table.find("tr").each(function() {
		        $(this).children("td:eq("+col_index+")").hide();
		      });
		    }else{
		     $(this).addClass('checked')
		     table.find("thead th:eq("+col_index+")") .show();
		     table.find("tr").each(function() {
		      $(this).children("td:eq("+col_index+")").show();
		    });
		   }
		 })
		  
		 $("#asbtn").off('click').on("click",function(){
		 	console.log(1)
		    $("em",this).hasClass('open') ? $("em",this).removeClass('open') : $("em",this).addClass('open');
		    $(".advanced-search").toggle(200);
		  })
	  
	}
	
	
	function loadDom(api,id,val) {
			ad.selectList(api, {},function(data) {
				if(val==''){
					$(id).empty()
				}else{
					$(id).empty().append("<option value='-1'> "+val+"</option>")
				}
				for(var i=0;i<data.length;i++){
					$(id).append("<option  value='"+data[i].id+"'>"+data[i].value+"</option>")
				}
				
				
			})
		}
	function loadDom2(data,id,a) {
			
				
					$(id).empty()
				
				if(a){
					
					for(var i=0;i<data.value.length;i++){
					$(id).append("<option mark='"+data.mark+"' value='"+data.value[i].values+"'>"+data.value[i].names+"</option>")
					}
				}
				if(!a){
					for(var i=0;i<data.length;i++){
					$(id).append("<option mark='"+data[i].mark+"' value='"+data[i].value+"'>"+data[i].name+"</option>")
					}
				}
				
				
			
		}
	function createParam(page,all) {
		
		
		var top1 = {
  			
			"dateRange":$("#DateRange").val(),//时间范围 
			"keyType":[$("#keyType").val(),$("#keyType").attr('mark')],//keyType	
			"keyValue":$("#searchVal").val()//查询关键词
  		}
  		var top2 = []
  		for(var i=0;i<idName.length;i++){
  		
  	
  			var obj = {name:idName[i],val:bigArry[i],mark:marks[i]}
  				
  			top2.push(obj)
  			
  		}
  		//top3的值
  		var inputsVal = []
  		var backTime = []
  		for(var i=0;i<$("#backVal input").length;i++){
  		
  
  			var val = $("#backVal").find("input").eq(i).val()
  			var val2 = $("#backIp").find("input").eq(i).val()
  				
  			inputsVal.push(val)
  			backTime.push(val2)
  			
  		}
		var top3 = {
			

			"result":[$("#result").val(),$("#result option").attr('mark')],//返回结果
			//"value":[{"flag":"backVal","val":inputsVal}],///返回值

		}
		var params = {
			"page":page,
			"top1":top1,
			"top2":top2,
			//"top3":top3,
				"firstShow":firstShow,
			"pageType":pageType,
			"pageSize": appApi('PAGE_SIZE')
		}
		if(all){
			var params = {
				"page":page,
				"top1":top1,
	//			"top2":top2,
	//			"top3":top3,
	//			"showNum":newLis,
		"firstShow":firstShow,
				"pageType":pageType,
				"pageSize": appApi('PAGE_SIZE')
			}
		}

		return params
	}
	
})